import com.ysb.deors.annotations.DeorsAnnotation;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.Set;


/*
An annotation processor is no more than a class that implements javax.annotation.processing.Processor interface and adheres to the given contract. For our convenience an abstract implementation with common functionality for custom processors is provided in the class javax.annotation.processing.AbstractProcessor.

The custom processor may use three annotations to configure itself:

javax.annotation.processing.SupportedAnnotationTypes: This annotation is used to register the annotations that the processor supports. Valid values are fully qualified names of annotation types – wildcards are allowed.
javax.annotation.processing.SupportedSourceVersion: This annotation is used to register the source version that the processor supports.
javax.annotation.processing.SupportedOptions: This annotation is used to register allowed custom options that may be passed through the command line.
 */
// @AutoService(Processor.class)
@SupportedAnnotationTypes({"com.ysb.deors.annotations.DeorsAnnotation"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
// javac -Aoption1=value1 -Aoption2=value2 MyFile.java
@SupportedOptions({"-option1", "-option2"})
public class DeorsProcessor extends AbstractProcessor {
  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // 获取传递给注解处理器的选项
    String option1 = processingEnv.getOptions().get("-option1");
    String option2 = processingEnv.getOptions().get("-option2");

    // 打印选项值
    System.out.println("Option 1: " + option1);
    System.out.println("Option 2: " + option2);

    for (Element elem : roundEnv.getElementsAnnotatedWith(DeorsAnnotation.class)) {
      DeorsAnnotation deorsAnnotation = elem.getAnnotation(DeorsAnnotation.class);
      String message = "annotation found in " + elem.getSimpleName()
        + " with complexity " + deorsAnnotation.value();
      processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message);
    }
    return true; // no further processing of this annotation type
  }
}
